package pers.zh.parking.mvp.presenter;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import pers.zh.parking.common.BaseObserver;
import pers.zh.parking.common.BaseResponse;
import pers.zh.parking.common.RxScheduler;
import pers.zh.parking.data.entity.ParkingLot;
import pers.zh.parking.data.entity.ParkingSpace;
import pers.zh.parking.mvp.contract.SpaceContract;
import pers.zjc.commonlibs.util.ObjectUtils;

@ActivityScope
public class SpacePresenter extends BasePresenter<SpaceContract.Model, SpaceContract.View> {

    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public SpacePresenter(SpaceContract.Model model, SpaceContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void loadData(long lotId) {
        mModel.getSpaces(lotId)
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse<List<ParkingSpace>>>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse<List<ParkingSpace>> response) {
                      List<ParkingSpace> list = response.getData();
                      mRootView.showData(list);
                  }
              });
    }

    public void delete(List<ParkingSpace> toDels) {
        if (ObjectUtils.isEmpty(toDels)) {
            mRootView.showMessage("删除项不能为空");
            return;
        }
        mModel.delete(toDels)
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse response) {
                      mRootView.deleteSuccess(toDels);
                  }
              });
    }

    public void add(ParkingLot lot) {
        ParkingSpace space = new ParkingSpace();
        space.setLotId(lot.getLotId());
        mModel.add(space)
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse response) {
                      mRootView.showMessage("新增成功");
                      mRootView.updateSuccess();
                  }
              });
    }

    public void update(ParkingSpace item) {
        mModel.update(item)
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse response) {
                      mRootView.showMessage("设置成功");
                      mRootView.updateSuccess();
                  }
              });
    }
}
